package org.cuit.lmq.config;

import lombok.extern.slf4j.Slf4j;
import org.cuit.lmq.domain.common.JacksonObjectMapper;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.util.List;

/**
 * @author Seon
 * @date 2023/3/6
 */
@Slf4j
@Configuration
public class WebMvcConfig implements WebMvcConfigurer {
    @Value("${pingpang_system.mapping}")
    private String mappingPath;
    /**
     * 配置可跨域
     */
    @Override
    public void addCorsMappings(CorsRegistry registry) {
        // 设置允许跨域的路径
        registry.addMapping("/**")
                // 允许跨域请求的域名
                .allowedOriginPatterns("*")
                // 允许cookie
                .allowCredentials(true)
                // 允许的方法种类
                .allowedMethods("GET", "POST", "DELETE", "PUT")
                // 允许的header的属性
                .allowedHeaders("*")
                // 跨域运行时间
                .maxAge(3600);
    }

    /**
     * 配置解析器，把数据格式规整一下
     */
    public void extendMessageConverters(List<HttpMessageConverter<?>> converters) {
        MappingJackson2HttpMessageConverter converter = new MappingJackson2HttpMessageConverter();
        converter.setObjectMapper(new JacksonObjectMapper());
        converters.add(0, converter);
    }
    /**
     * 进行路径映射的时候一定要死死的记住路径拼接的后面需要加上一个 / 不然是匹配不到的！！！
     * 已经替换为了nginx作为图片服务器
     * @param registry
     */
//    public void addResourceHandlers(ResourceHandlerRegistry registry) {
//        log.info("LiangMiaoqi reminding you:The resource loader is working...");
//        // appendix负责存储文章中的附件（图片还有封面）
//        registry.addResourceHandler("/appendix/**").addResourceLocations("file:" + mappingPath + "/appendix/");
//        // avatar是头像
//        registry.addResourceHandler("/avatar/**").addResourceLocations("file:" + mappingPath + "/avatar/");
//        // photo是照片墙
//        registry.addResourceHandler("/photo/**").addResourceLocations("file:" + mappingPath + "/photo/");
//        // cover是照片墙
//        registry.addResourceHandler("/cover/**").addResourceLocations("file:" + mappingPath + "/cover/");
//        log.info("LiangMiaoqi reminding you:Resource loader was completed!");
//    }
}